﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using YunSpace.Core.Pdf.Params;

namespace YunSpace.Core.Pdf
{
    public class PdfProvider
    {
        private int _width;
        private int _height;
        private readonly string _toSaveFileName;
        private PdfWriter _pdfWriter;

        private static readonly BaseFont basefont = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        private static readonly Font font = new iTextSharp.text.Font(basefont, 8, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

        public PdfProvider(string toSaveFileName, int width = 500, int height = 300)
        {
            _width = width;
            _height = height;
            Document = new Document(new Rectangle(width, height));
            _toSaveFileName = toSaveFileName;
        }

        public Document Document { get; }

        /// <summary>
        /// 绘画
        /// </summary>
        public void Draw(string imgName, CoordinateParam coordinate, iTextSharp.text.pdf.PdfRectangle rectangle)
        {
            if (_pdfWriter == null)
            {
                _pdfWriter = PdfWriter.GetInstance(Document, new FileStream(_toSaveFileName, FileMode.Create));

            }

            if (!Document.IsOpen())
            {
                Document.Open();
            }



            var img = iTextSharp.text.Image.GetInstance(imgName);
            img.SetAbsolutePosition(coordinate.X, coordinate.Y);
            img.ScaleAbsoluteWidth(rectangle.Width);
            img.ScaleAbsoluteHeight(rectangle.Height);

            _pdfWriter.DirectContent.AddImage(img);
        }

        /// <summary>
        /// 绘画
        /// </summary>
        /// <param name="imgName"></param>
        /// <param name="absoluteX"></param>
        /// <param name="absoluteY"></param>
        /// <param name="absoluteWidth"></param>
        /// <param name="absoluteHeight"></param>
        public void Draw(string imgName, float absoluteX = 0, float absoluteY = 0, float absoluteWidth = 550, float absoluteHeight = 15)
        {
            if (_pdfWriter == null)
            {
                _pdfWriter = PdfWriter.GetInstance(Document, new FileStream(_toSaveFileName, FileMode.Create));

            }

            if (!Document.IsOpen())
            {
                Document.Open();
            }

            var img = iTextSharp.text.Image.GetInstance(imgName);
            img.SetAbsolutePosition(absoluteX, absoluteY);
            img.ScaleAbsoluteWidth(absoluteWidth);
            img.ScaleAbsoluteHeight(absoluteHeight);

            _pdfWriter.DirectContent.AddImage(img);
        }

        public void DrawContent(string content, int fontSize = 18, int red = 0, int green = 0, int blue = 0, float x = 0, float y = 0, float rotation = 0)
        {
            var cb = _pdfWriter.DirectContent;
            cb.BeginText();
            BaseFont bfont = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设定字体：黑体 
            cb.SetFontAndSize(bfont, fontSize);//设定字号 
            //cb.SetCharacterSpacing(1);//设定字间距 
            cb.SetRGBColorFill(red, green, blue);//设定文本颜色 
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, content, x, y, rotation);
            cb.EndText();


        }

        public void AddToDocument(IElement element)
        {
            Document.Add(element);
        }

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            Document.Close();
            Document.Dispose();

            //_pdfWriter.Close();
            //_pdfWriter.Dispose();
        }

        /// <summary>
        /// 创建单元格
        /// </summary>
        /// <returns></returns>
        public PdfPCell CreateCell(string content, CellStyleParam style)
        {
            var cell = new PdfPCell(new Phrase(content, new iTextSharp.text.Font(basefont, style.FontSize, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0))))
            {
                VerticalAlignment = style.VerticalAlignment,
                HorizontalAlignment = style.HorizontalAlignment,
                Colspan = style.Colspan,
                Rowspan = style.Rowspan,
                Border = style.BorderWith
            };

            return cell;
        }

        /// <summary>
        /// 创建单元格
        /// </summary>
        /// <param name="content"></param>
        /// <param name="colspan"></param>
        /// <param name="rowspan"></param>
        /// <param name="verticalAlignment"></param>
        /// <param name="horizontalAlignment"></param>
        /// <param name="fontSize"></param>
        /// <param name="borderWith"></param>
        /// <param name="rgb"></param>
        /// <returns></returns>
        public PdfPCell CreateCell(string content, int colspan = 1, int rowspan = 1, int verticalAlignment = Element.ALIGN_MIDDLE, int horizontalAlignment = Element.ALIGN_CENTER, int fontSize = 8, int borderWith = 1, RgbParam rgb = null)
        {
            rgb = rgb ?? new RgbParam();
            var cell = new PdfPCell(new Phrase(content.IsNullOrEmpty() ? " " : content, new iTextSharp.text.Font(basefont, fontSize, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(rgb.Red, rgb.Green, rgb.Blue))))
            {
                VerticalAlignment = verticalAlignment,
                HorizontalAlignment = horizontalAlignment,
                Colspan = colspan,
                Rowspan = rowspan,
                Border = borderWith
            };

            return cell;
        }

        /// <summary>
        /// 创建包含空格的单元格
        /// </summary>
        /// <returns></returns>
        public PdfPCell CreateBlankSpaceCell(int colspan = 1, int rowspan = 1, int fontSize = 8, int borderWith = 0)
        {
            var cell = new PdfPCell(new Phrase(" ", new iTextSharp.text.Font(basefont, fontSize, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0))))
            {
                Colspan = colspan,
                Rowspan = rowspan,
                Border = borderWith
            };

            return cell;
        }


        /// <summary>
        /// 创建空单元格
        /// </summary>
        /// <returns></returns>
        public PdfPCell CreateEmptyCell(int colspan = 1, int rowspan = 1)
        {
            return new PdfPCell
            {
                Rowspan = rowspan,
                Colspan = colspan,
                Border = 0,
            };
        }

        /// <summary>
        /// 创建图片单元格
        /// </summary>
        /// <returns></returns>
        public PdfPCell CreateImgCell(string fileName, int colspan = 1, int rowspan = 1, int verticalAlignment = Element.ALIGN_CENTER, int fixedHeight = 0)
        {
            PdfPCell cell = null;

            if (fixedHeight == 0)
            {
                cell = new PdfPCell(iTextSharp.text.Image.GetInstance(fileName), true)
                {
                    Colspan = colspan,
                    Rowspan = rowspan,
                    VerticalAlignment = verticalAlignment,
                    Border = 0
                };
            }
            else
            {
                cell = new PdfPCell(iTextSharp.text.Image.GetInstance(fileName), true)
                {
                    Colspan = colspan,
                    Rowspan = rowspan,
                    VerticalAlignment = verticalAlignment,
                    Border = 0,
                    FixedHeight = 100
                };

            }

            return cell;
        }

        /// <summary>
        /// 创建表格
        /// </summary>
        /// <param name="numColumns"></param>
        /// <param name="totalWidth"></param>
        /// <param name="lockedWith"></param>
        /// <returns></returns>
        public PdfPTable CreateTable(int numColumns, int totalWidth = 450, bool lockedWith = true)
        {
            return new PdfPTable(numColumns)
            {
                TotalWidth = totalWidth,
                LockedWidth = lockedWith
            };
        }
    }

}
